package cn.huihui.miniprogram.stuman.service;

import cn.huihui.miniprogram.stuman.database.mapper.AttendanceMapper;
import cn.huihui.miniprogram.stuman.database.mapper.ExamMapper;
import cn.huihui.miniprogram.stuman.database.mapper.ScoreMapper;
import cn.huihui.miniprogram.stuman.database.mapper.StudentMapper;
import cn.huihui.miniprogram.stuman.database.model.Attendance;
import cn.huihui.miniprogram.stuman.database.model.Exam;
import cn.huihui.miniprogram.stuman.database.model.Score;
import cn.huihui.miniprogram.stuman.database.model.Student;
import cn.huihui.miniprogram.stuman.dto.AttendanceDto;
import cn.huihui.miniprogram.stuman.dto.ExamArrangementDto;
import cn.huihui.miniprogram.stuman.dto.ScoreDto;
import cn.huihui.miniprogram.stuman.dto.UserInfoDto;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务层
 */
@Slf4j
@Service
public class AllService {

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    ExamMapper examMapper;

    @Autowired
    ScoreMapper scoreMapper;

    @Autowired
    AttendanceMapper attendanceMapper;


    public ScoreDto searchScoreOfCourseName(String type){
        Score score = scoreMapper.selectByStudentIdAndCourse(currentLoginStudentId(), type);
        Assert.notNull(score,"您没有必修/选修此课程");
        ScoreDto scoreDto = new ScoreDto();
        BeanUtils.copyProperties(score,scoreDto);
        return scoreDto;
    }

    public List<AttendanceDto> getAttendance(){
        List<Attendance> attendanceList = attendanceMapper.selectByStudentId(currentLoginStudentId());
        ArrayList<AttendanceDto> dtoArrayList = new ArrayList<>(attendanceList.size());
        for (int i = 0; i < attendanceList.size(); i++) {
            Attendance attendance = attendanceList.get(i);
            AttendanceDto dto = new AttendanceDto();
            dtoArrayList.add(dto);
            BeanUtils.copyProperties(attendance,dto);
        }
        return dtoArrayList;
    }

    public List<ExamArrangementDto> getExamArrange(){
        List<Exam> examList = examMapper.selectByStudentId(currentLoginStudentId());
        ArrayList<ExamArrangementDto> dtoArrayList = new ArrayList<>(examList.size());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
        for (int i = 0; i < examList.size(); i++) {
            Exam exam = examList.get(i);
            ExamArrangementDto dto = new ExamArrangementDto();
            dtoArrayList.add(dto);
            dto.setId(i + 1);
            // 您最近的考试：英语四级\n
            // 考场：广东技术师范大学一教404\n
            // 考号：2020202013131\n
            // 考试时间：2020年12月3日12：00"
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("您最近的考试").append(':').append(exam.getCourse()).append('\n').
                    append("考场").append(":").append(exam.getClassroom()).append('\n').
                    append("考号").append(":").append(exam.getExamNo()).append('\n').
                    append("考试时间").append(":").append(dateFormat.format(exam.getDatetime())).append('\n');
            dto.setCtx(stringBuffer.toString());
        }
        return dtoArrayList;
    }

    public UserInfoDto currentUserInfo(){
        String id = currentLoginStudentId();
        return getStudentInfo(id);
    }

    public UserInfoDto modifyUserInfo(UserInfoDto userInfoDto){
        log.info("params: {}",JSON.toJSONString(userInfoDto));
        Student student = studentMapper.selectByStudentId(currentLoginStudentId());
        Assert.notNull(student,"您还没有登录");
        log.info("selected obj:{}",JSON.toJSONString(student));
        BeanUtils.copyProperties(userInfoDto,student);
        studentMapper.updateByPrimaryKey(student);
        return userInfoDto;
    }


    public UserInfoDto getStudentInfo(String id) {
        Student student = studentMapper.selectByStudentId(id);
        Assert.notNull(student,"没有此学生");
        log.info("selected obj:{}",JSON.toJSONString(student));
        UserInfoDto userInfoDto = new UserInfoDto();
        BeanUtils.copyProperties(student,userInfoDto);
        return userInfoDto;
    }

    private String currentLoginStudentId(){
        return (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
